﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Models.BusinessObject;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class OrganizationController : BaseController
    {
        //
        // GET: /Organization/

        public ActionResult Index()
        {
            ViewData["ajax"] =  true;
            ViewData["scrolling"] =  true;
            ViewData["paging"] =  true;
            ViewData["filtering"] =  true;
            ViewData["grouping"] =  true;
            ViewData["sorting"] =  true;
            ViewData["showFooter"] = true;
            return View(OrganizationBOL.GetAllOrganization());
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(OrganizationBOL.GetAllOrganization()));
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View(new Organization());
        }

        [HttpPost]
        public ActionResult Add(Organization model)
        {
            if (ModelState.IsValid)
            {
                model.Name.Trim();
                OrganizationBOL.Insert(model);
            }
            //return View(model);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int organizationID)
        {
            Organization model = OrganizationBOL.GetOrganizationById(organizationID);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(Organization model)
        {
            if (ModelState.IsValid)
            {
                model.Name.Trim();
                OrganizationBOL.Update(model);
            }
            //return View(model);
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record        
                if (!OrganizationBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This Organization have Country. Please check again!");
                }
            }

            //Rebind the grid
            return View(new GridModel(OrganizationBOL.GetAllOrganization()));
        }
    }
}
